package com.sharemedia.views
{
	import com.sharemedia.events.PrefEvent;
	import com.sharemedia.events.TrackEvent;
	import com.sharemedia.interfaces.IAudioManager;
	import com.sharemedia.managers.TrackList;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import spark.components.Button;
	import spark.components.ToggleButton;
	import spark.components.supportClasses.SkinnableComponent;
	import spark.components.supportClasses.SliderBase;
	
	public class Player extends SkinnableComponent
	{
		[SkinPart]
		public var playPauseButton:ToggleButton;
		
		[SkinPart]
		public var muteButton:ToggleButton;
		
		[SkinPart]
		public var shufleButton:ToggleButton;
		
		[SkinPart]
		public var nextButton:Button;
		
		[SkinPart]
		public var prevButton:Button;
		
		[SkinPart]
		public var likeButton:Button;
		
		[SkinPart]
		public var unlikeButton:Button;
		
		[SkinPart]
		public var prefButton:Button;
		
		[SkinPart]
		public var prefDialog:PrefDialog;
		
		[SkinPart]
		public var volumeSlider:SliderBase;
		
		[SkinPart]
		public var playList:Playlist;
		
		[SkinPart]
		public var promptList:PromptList;
		
		[SkinPart(required="true")]
		public var audioManager:IAudioManager;
		
		[SkinPart(required="false")]
		/**
		 *  A skin part that defines the sound visualization component. 
		 */
		public var visualization:SoundVisualization;
		
		public function Player()
		{
			super();
		}
		
		override protected function partAdded(partName:String, instance:Object):void
		{
			super.partAdded(partName, instance);
			
			switch(instance)
			{
				case playPauseButton:
				{
					playPauseButton.addEventListener(MouseEvent.CLICK, playPauseClickHandler);
					playPauseButton.enabled = connected;
					break;
				}
				
				case muteButton:
				{
					muteButton.addEventListener(MouseEvent.CLICK, muteClickHandler);
					break;
				}
				
				case shufleButton:
				{
					shufleButton.addEventListener(MouseEvent.CLICK, shufleClickHandler);
					break;
				}
					
				case nextButton:
				{
					nextButton.addEventListener(MouseEvent.CLICK, nextClickHandler);
					nextButton.enabled = connected;
					break;
				}
					
				case prevButton:
				{
					prevButton.addEventListener(MouseEvent.CLICK, prevClickHandler);
					prevButton.enabled = connected;
					break;
				}
					
				case likeButton:
				{
					likeButton.addEventListener(MouseEvent.CLICK, likeClickHandler);
					likeButton.enabled = audioManager ? audioManager.isPlaying() : false;
					break;
				}
					
				case unlikeButton:
				{
					unlikeButton.addEventListener(MouseEvent.CLICK, unlikeClickHandler);
					unlikeButton.enabled = audioManager ? audioManager.isPlaying() : false;
					break;
				}
					
				case prefButton:
				{
					prefButton.addEventListener(MouseEvent.CLICK, prefClickHandler);
					break;
				}
					
				case prefDialog:
				{
					prefDialog.addEventListener(PrefEvent.SAVE, prefSaveHandler);
					break;
				}
					
				case volumeSlider:
				{
					volumeSlider.addEventListener(Event.CHANGE, volumeChangeHandler);
					
					if(audioManager)
						volumeSlider.value = audioManager.volume;
					
					break;
				}
					
				case audioManager:
				{
					if(playList)
						playList.audioManager = audioManager;
					
					if(promptList)
						promptList.audioManager = audioManager;
					
					if(volumeSlider)
						volumeSlider.value = audioManager.volume;
					
					if(likeButton)
						likeButton.enabled = audioManager.isPlaying();
					if(unlikeButton)
						unlikeButton.enabled = audioManager.isPlaying();
					
					audioManager.addEventListener(TrackEvent.PLAY, playHandler);
					audioManager.addEventListener(TrackEvent.PAUSE, pauseHandler);
					audioManager.addEventListener("musicStreamConnected", musicStreamConnectedHandler);
					audioManager.init("P2P Shared Media");
					break;
				}
					
				case playList:
				{
					if(audioManager)
						playList.audioManager = audioManager;
					break;
				}
					
				case promptList:
				{
					if(promptList)
						promptList.audioManager = audioManager;
					break;
				}
			}
		}
		
		[Bindable]
		public var connected:Boolean;
		
		private function musicStreamConnectedHandler(event:Event):void
		{
			connected = true;
			
			if(playPauseButton)
				playPauseButton.enabled = true;
			if(nextButton)
				nextButton.enabled = true;
			if(prevButton)
				prevButton.enabled = true;
			
			audioManager.play();
		}
		
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			
			switch(instance)
			{
				case playPauseButton:
				{
					playPauseButton.removeEventListener(MouseEvent.CLICK, playPauseClickHandler);
					break;
				}
					
				case muteButton:
				{
					muteButton.removeEventListener(MouseEvent.CLICK, muteClickHandler);
					break;
				}
					
				case shufleButton:
				{
					shufleButton.removeEventListener(MouseEvent.CLICK, shufleClickHandler);
					break;
				}
					
				case nextButton:
				{
					nextButton.removeEventListener(MouseEvent.CLICK, nextClickHandler);
					break;
				}
					
				case prevButton:
				{
					prevButton.removeEventListener(MouseEvent.CLICK, prevClickHandler);
					break;
				}
					
				case likeButton:
				{
					likeButton.removeEventListener(MouseEvent.CLICK, likeClickHandler);
					break;
				}
					
				case unlikeButton:
				{
					unlikeButton.removeEventListener(MouseEvent.CLICK, unlikeClickHandler);
					break;
				}
					
				case prefButton:
				{
					prefButton.removeEventListener(MouseEvent.CLICK, prefClickHandler);
					break;
				}
					
				case prefDialog:
				{
					prefDialog.addEventListener(PrefEvent.SAVE, prefSaveHandler);
					break;
				}
					
				case volumeSlider:
				{
					volumeSlider.removeEventListener(Event.CHANGE, volumeChangeHandler);
					break;
				}
					
				case audioManager:
				{
					audioManager.removeEventListener(TrackEvent.PLAY, playHandler);
					audioManager.removeEventListener(TrackEvent.PAUSE, pauseHandler);
					audioManager.removeEventListener("musicStreamConnected", musicStreamConnectedHandler);
					break;
				}
			}
		}
		
		private function playHandler(event:TrackEvent):void
		{
			if(visualization)
				visualization.play();
			
			if(likeButton)
				likeButton.enabled = true;
			if(unlikeButton)
				unlikeButton.enabled = true;
			
			if(playPauseButton)
				playPauseButton.selected = true;
		}
		
		private function pauseHandler(event:TrackEvent):void
		{
			if(visualization)
				visualization.stop();
			
			if(playPauseButton)
				playPauseButton.selected = false;
			
			if(likeButton)
				likeButton.enabled = false;
			if(unlikeButton)
				unlikeButton.enabled = false;
		}
		
		protected function playPauseClickHandler(event:MouseEvent):void
		{
			if(playPauseButton.selected)
				audioManager.play();
			else
				audioManager.pause();
		}
		
		private var lastVolume:Number;
		protected function muteClickHandler(event:MouseEvent):void
		{
			if(muteButton.selected)
			{
				lastVolume = audioManager.volume;
				volumeSlider.value = 0;
			}
			else
			{
				volumeSlider.value = lastVolume > 0 ? lastVolume : 1;
			}
			
			volumeChangeHandler();
		}
		
		protected function shufleClickHandler(event:MouseEvent):void
		{
			audioManager.shuffle = shufleButton.selected;
		}
		
		protected function nextClickHandler(event:MouseEvent):void
		{
			audioManager.nextTrack();
		}
		
		protected function prevClickHandler(event:MouseEvent):void
		{
			audioManager.prevTrack();
		}
		
		protected function volumeChangeHandler(event:Event=null):void
		{
			audioManager.volume = volumeSlider.value;
			
			muteButton.selected = audioManager.volume == 0;
		}
		
		protected function likeClickHandler(event:MouseEvent):void
		{
			audioManager.like();
			likeButton.enabled = false;
			unlikeButton.enabled = false;
		}
		
		protected function unlikeClickHandler(event:MouseEvent):void
		{
			audioManager.unlike();
			likeButton.enabled = false;
			unlikeButton.enabled = false;
		}
		
		protected function prefClickHandler(event:MouseEvent):void
		{
			if(prefDialog)
			{
				prefDialog.serverName = audioManager.serverName;
				prefDialog.bufferSize= audioManager.bufferSize;
				prefDialog.show(this);
			}
		}
		
		protected function prefSaveHandler(event:PrefEvent):void
		{
			audioManager.pause();
			audioManager.bufferSize = event.bufferSize;
			audioManager.init(event.serverName);
			
			connected = false;
			
			if(playPauseButton)
				playPauseButton.enabled = false;
			if(nextButton)
				nextButton.enabled = false;
			if(prevButton)
				prevButton.enabled = false;
		}
	}
}